package com.hzy.fivepalace.db.service;

import com.hzy.fivepalace.common.util.ResponseUtil;
import com.hzy.fivepalace.db.dao.DtsGoodsSecurityMapper;
import com.hzy.fivepalace.db.domain.DtsGoodsSecurity;
import com.hzy.fivepalace.db.domain.DtsGoodsSecurityExample;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 商品保障服务
 */
@Service
public class DtsGoodsSecurityService {

    @Resource
    private DtsGoodsSecurityMapper goodsSecurityMapper;

    @Resource
    private Redisson redisson;

    /**
     * 获取商品保障列表
     *
     * @return
     */
    public List<DtsGoodsSecurity> selectList() {
        DtsGoodsSecurityExample example = new DtsGoodsSecurityExample();
        example.setOrderByClause("sort asc,create_time desc");
        example.createCriteria().andDeletedEqualTo(false);
        List<DtsGoodsSecurity> list = goodsSecurityMapper.selectByExample(example);
        return list;
    }

    /**
     * 新增商品保障
     *
     * @param goodsSecurity
     * @param adminId
     * @return
     */
    public Object addGoodsSecurity(DtsGoodsSecurity goodsSecurity, Integer adminId) {
        RLock lock = redisson.getLock("admin_add_goods_security_" + adminId);
        if (lock.isLocked()) {
            return ResponseUtil.frequent();
        }
        lock.lock(30, TimeUnit.SECONDS);
        if (StringUtils.isBlank(goodsSecurity.getTitle())) {
            lock.unlock();
            return ResponseUtil.fail("标题不能为空");
        }
        if (goodsSecurity.getTitle().length() > 10) {
            lock.unlock();
            return ResponseUtil.fail("标题最多10个字");
        }
        if (StringUtils.isBlank(goodsSecurity.getContent())) {
            lock.unlock();
            return ResponseUtil.fail("内容不能为空");
        }
        if (goodsSecurity.getContent().length() > 250) {
            lock.unlock();
            return ResponseUtil.fail("内容最多250个字");
        }
        goodsSecurity.setCreateTime(LocalDateTime.now());
        goodsSecurity.setDeleted(false);
        goodsSecurityMapper.insertSelective(goodsSecurity);
        lock.unlock();
        return ResponseUtil.ok("新增成功",null);
    }

    /**
     * 修改商品保障
     *
     * @param goodsSecurity
     * @return
     */
    public Object updGoodsSecurity(DtsGoodsSecurity goodsSecurity) {
        if (null == goodsSecurity.getId() || goodsSecurity.getId().intValue() < 1) {
            return ResponseUtil.fail("参数【id】为空");
        }
        RLock lock = redisson.getLock("admin_upd_goods_security_" + goodsSecurity.getId());
        if (lock.isLocked()) {
            return ResponseUtil.frequent("其他管理人员操作中,请稍后再试");
        }
        lock.lock(30, TimeUnit.SECONDS);
        if (StringUtils.isBlank(goodsSecurity.getTitle())) {
            goodsSecurity.setTitle(null);
        }
        if (!StringUtils.isBlank(goodsSecurity.getTitle()) && goodsSecurity.getTitle().length() > 10) {
            lock.unlock();
            return ResponseUtil.fail("标题最多10个字");
        }
        if (StringUtils.isBlank(goodsSecurity.getContent())) {
            goodsSecurity.setContent(null);
        }
        if (!StringUtils.isBlank(goodsSecurity.getContent()) && goodsSecurity.getContent().length() > 250) {
            lock.unlock();
            return ResponseUtil.fail("内容最多250个字");
        }

        DtsGoodsSecurity security = goodsSecurityMapper.selectByPrimaryKey(goodsSecurity.getId());
        if (null == security || security.getDeleted()) {
            lock.unlock();
            return ResponseUtil.ok("数据不存在或已被删除");
        }
        goodsSecurity.setCreateTime(null);
        goodsSecurity.setDeleted(null);
        goodsSecurityMapper.updateByPrimaryKeySelective(goodsSecurity);
        lock.unlock();
        return ResponseUtil.ok("修改成功",null);
    }

    /**
     * 删除商品保障
     *
     * @param id
     * @return
     */
    public Object delGoodsSecurity(Integer id) {
        RLock lock = redisson.getLock("admin_del_goods_security" + id);
        if (lock.isLocked()) {
            return ResponseUtil.frequent("其他管理人员操作中,请稍后再试");
        }
        lock.lock(30, TimeUnit.SECONDS);
        DtsGoodsSecurity security = goodsSecurityMapper.selectByPrimaryKey(id);
        if (null == security || security.getDeleted()) {
            lock.unlock();
            return ResponseUtil.ok("删除成功",null);
        }
        security.setDeleted(true);
        goodsSecurityMapper.updateByPrimaryKeySelective(security);
        lock.unlock();
        return ResponseUtil.ok("删除成功",null);
    }


}
